﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;
using HYFloorPipeModule;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HeatingSystemCal : HYNoTranExternalCommand
	{
		public override Result HYExecute()
		{
			Result result;
			try
			{
				SearchSystem.LogEnabled = false;
				Log.Enabled = false;
				UIDocument activeUIDocument = this.commandData.Application.ActiveUIDocument;
				string title = activeUIDocument.Document.Title;
				SavePipe.FileName = ((!title.Contains(".rvt")) ? title : title.Substring(0, title.LastIndexOf(".rvt")));
				MainForm mainForm = new MainForm(activeUIDocument);
				DialogResult dialogResult = mainForm.ShowDialog();
				while (dialogResult == DialogResult.Retry || dialogResult == DialogResult.Abort)
				{
					if (dialogResult == DialogResult.Abort)
					{
						this.GetSystem(base.Doc, base.Sel, mainForm.Building);
					}
					else
					{
						mainForm = new MainForm(activeUIDocument);
					}
					dialogResult = mainForm.ShowDialog();
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public override void FilterProcess()
		{
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)4);
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)1);
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)2);
			base.FilterProcess();
		}

		private void GetSystem(Document doc, Selection sel, BuildingSystem buildSystem)
		{
			try
			{
				if (buildSystem != null)
				{
					string[] lineNames = this.GetLineNames(doc);
					if (lineNames == null)
					{
						YJKMessageBox.Show("没有线样式，请新建线样式。");
					}
					else
					{
						Dictionary<int, string> dictionary = new Dictionary<int, string>();
						Dictionary<int, LevelMarkInfo> dictionary2 = new Dictionary<int, LevelMarkInfo>();
						int count = buildSystem.SupplyPipes.Count;
						for (int i = 1; i <= count; i++)
						{
							dictionary.Add(i, "DN" + buildSystem.SupplyPipes.At(i).DN);
						}
						for (int j = 1; j <= count; j++)
						{
							int index = Math.Abs(j - (1 - buildSystem.ConnectType) * (count + 1));
							dictionary.Add(count + j, "DN" + buildSystem.BackPipes.At(index).DN);
						}
						for (int k = 0; k < buildSystem.RisePipes.Count; k++)
						{
							int count2 = buildSystem.GetAt(k + 1).FloorSystems.Count;
							Dictionary<int, string> dictionary3 = new Dictionary<int, string>();
							Dictionary<int, int> dictionary4 = new Dictionary<int, int>();
							for (int l = 1; l <= count2; l++)
							{
								dictionary3.Add(l, "DN" + buildSystem.GetAt(k + 1).SupplyPipes.At(l).DN);
								dictionary4.Add(l, buildSystem.GetAt(k + 1).GetAt(l).InDoorSystem.Count);
								Log.Info(new string[]
								{
									string.Format("立管{0}楼层{1}的分户数{2}", k + 1, l, buildSystem.GetAt(k + 1).GetAt(l).InDoorSystem.Count)
								});
							}
							for (int m = 1; m <= count2; m++)
							{
								dictionary3.Add(count2 + m, "DN" + buildSystem.GetAt(k + 1).BackPipes.At(count2 - m + 1).DN);
							}
							dictionary2.Add(k + 1, new LevelMarkInfo(count2, dictionary3, dictionary4));
						}
						HeatingPrincipleDiagramPrameters heatingPrincipleDiagramPrameters = new HeatingPrincipleDiagramPrameters();
						heatingPrincipleDiagramPrameters.isSameDisOut = (buildSystem.ConnectType == 0);
						heatingPrincipleDiagramPrameters.isSameDisIn = (buildSystem.GetAt(1).ConnectType == 0);
						heatingPrincipleDiagramPrameters.stanPipeCount = buildSystem.RisePipes.Count;
						heatingPrincipleDiagramPrameters.dic = dictionary;
						heatingPrincipleDiagramPrameters.dicLevelMarkInfo = dictionary2;
						XYZ basepoint = sel.PickPoint("选择一个插入点");
						Transaction transaction = new Transaction(doc, "绘制原理图");
						transaction.Start();
						new DrawSampleChart(doc, doc.ActiveView).DrawTheSampleChart(basepoint, lineNames, heatingPrincipleDiagramPrameters);
						doc.Regenerate();
						transaction.Commit();
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
			}
		}

		private string[] GetLineNames(Document doc)
		{
			string[] array = new string[]
			{
				"旋转轴",
				"<房间分隔>",
				"<面积边界>",
				"线",
				"隐藏线",
				"细线"
			};
			List<string> list = (from gs in new FloorPipeTools(doc).GetLineStyle()
			select gs.Name).ToList<string>();
			if (!list.Contains("线"))
			{
				return null;
			}
			if (!list.Contains("旋转轴"))
			{
				array[0] = "线";
			}
			if (!list.Contains("<房间分隔>"))
			{
				array[1] = "线";
			}
			if (!list.Contains("<面积边界>"))
			{
				array[2] = "线";
			}
			if (!list.Contains("隐藏线"))
			{
				array[4] = "线";
			}
			if (!list.Contains("细线"))
			{
				array[5] = "线";
			}
			return array;
		}
	}
}
